﻿using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Bricks;
using Bricks.Injecting;

namespace Motar.ServiceModel
{

    /// <summary>
    /// 
    /// </summary>
    public class CookieEndpointBehavior : FacadeBase<IValueResolver<ICookieMessageInspector>>, IEndpointBehavior
    {

        /// <summary>
        /// 
        /// </summary>
        public CookieEndpointBehavior() :
            this(new SingleValueResolver<ICookieMessageInspector, CookieMessageInspector>())
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public CookieEndpointBehavior(string key) :
            this(new SingleValueResolver<ICookieMessageInspector, CookieMessageInspector>(key))
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerFacadeResolver"></param>
        public CookieEndpointBehavior(IValueResolver<ICookieMessageInspector> cookieMessageInspectorResolver) :
            base(() => cookieMessageInspectorResolver)
        {
            if (cookieMessageInspectorResolver == null)
            {
                throw new ArgumentNullException("cookieMessageInspectorResolver");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="bindingParameters"></param>
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="clientRuntime"></param>
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            Execute(cookieMessageInspectorResolver => clientRuntime.MessageInspectors.Add(cookieMessageInspectorResolver.Resolve()));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="endpointDispatcher"></param>
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="endpoint"></param>
        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
}
